﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mshtml;   

namespace BookInfoExtractor
{
    public static class BookPattern
    {
        private static HtmlElement _author;
        private static HtmlElement _title;
        private static HtmlElement _image;
        private static HtmlElement _isbn;
        private static HtmlElement _price;

        private static int level = -1;
        //private static int posIndex = 0;
        private static string nodePosition = string.Empty;
        private static List<int> lstTrackPos = new List<int>();
        private static List<int> lstAuthorPos = new List<int>();
        private static List<int> lstTitlePos = new List<int>();
        private static List<int> lstImagePos = new List<int>();
        private static List<int> lstISBNPos = new List<int>();
        private static List<int> lstPricePos = new List<int>();

        private static bool isFound = false;

        public static HtmlElement Element { get; set; }
        public static HtmlElement Author
        { 
            get
            {
                return _author;
            } 
            set
            {
                _author = value;
                lstTrackPos.Clear();
                isFound = false;
                SetHierarchicalPosition(Element, _author);
                lstAuthorPos = lstTrackPos.ToList();
                lstAuthorPos.Reverse();  
            } 
        }

        public static HtmlElement Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                lstTrackPos.Clear();
                isFound = false;
                SetHierarchicalPosition(Element, _title);
                lstTitlePos = lstTrackPos.ToList();
                lstTitlePos.Reverse();
            }
        }

        public static HtmlElement Image
        {
            get
            {
                return _image;
            }
            set
            {
                _image = value;
                lstTrackPos.Clear();
                isFound = false;
                SetHierarchicalPosition(Element, _image, true);
                lstImagePos = lstTrackPos.ToList();
                lstImagePos.Reverse();
            }
        }
        public static HtmlElement ISBN
        {
            get
            {
                return _isbn;
            }
            set
            {
                _isbn = value;
                lstTrackPos.Clear();
                isFound = false;
                SetHierarchicalPosition(Element, _isbn);
                lstISBNPos = lstTrackPos.ToList();
                lstISBNPos.Reverse();
            }
        }

        public static HtmlElement Price
        {
            get
            {
                return _price;
            }
            set
            {
                _price = value;
                lstTrackPos.Clear();
                isFound = false;
                SetHierarchicalPosition(Element, _price);
                lstPricePos = lstTrackPos.ToList();
                lstPricePos.Reverse();
            }
        }

        public static List<int> GetAuthorPosition()
        {
            return lstAuthorPos; 
        }

        public static List<int> GetTitlePosition()
        {
            return lstTitlePos;
        }

        public static List<int> GetImagePosition()
        {
            return lstImagePos;
        }

        public static List<int> GetISBNPosition()
        {
            return lstISBNPos;
        }

        public static List<int> GetPricePosition()
        {
            return lstPricePos;
        }

        private static void SetHierarchicalPosition(HtmlElement _subElement, HtmlElement _searchNode, bool isImg = false)
        {
            HtmlElement topNode;
            topNode = _subElement; 
            //if (level == 0)
            //    topNode = Element;
            //else
            //    topNode = _subElement;

            int posIndex = 0;
            foreach (HtmlElement child in topNode.Children)
            {
                if (isFound)
                    break;

                if (isImg)
                {
                    //child is image, then check if src matches
                    //if matched then found
                    //else recursive as below

                    if ((child.DomElement is IHTMLImgElement) && (((IHTMLImgElement)child.DomElement).src == ((IHTMLImgElement)_searchNode.DomElement).src))
                    {
                        nodePosition = nodePosition + posIndex + "-" + child.TagName + ".";
                        lstTrackPos.Add(posIndex);
                        MessageBox.Show(child.InnerText);
                        isFound = true;
                        break;
                    }
                    else
                    {
                        SetHierarchicalPosition(child, _searchNode, true);
                        if (isFound)
                        {
                            nodePosition = nodePosition + posIndex + "-" + child.TagName + ".";
                            lstTrackPos.Add(posIndex);
                        }
                    }

                }
                else
                {
                    if (child.InnerText != _searchNode.InnerText)
                    {
                        SetHierarchicalPosition(child, _searchNode);
                        if (isFound)
                        {
                            nodePosition = nodePosition + posIndex + "-" + child.TagName + ".";
                            lstTrackPos.Add(posIndex);
                        }
                    }
                    else
                    {
                        nodePosition = nodePosition + posIndex + "-" + child.TagName + ".";
                        lstTrackPos.Add(posIndex);
                        MessageBox.Show(child.InnerText);
                        isFound = true;
                        break;
                    }
                }
                posIndex++;
            }
        }

    }
}
